package com.gxa.uhome.controller;

import com.gxa.uhome.mapper.RoleMapper;
import com.gxa.uhome.pojo.Admin;
import com.gxa.uhome.pojo.Role;
import com.gxa.uhome.service.RoleService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: qingyao
 * @date: 12:45 2021/6/5
 */
@RestController
@RequestMapping("/role")
@Api(value = "角色管理模块",produces = "application/json;charset=UTF-8",tags = "Role角色管理模块")
public class RoleController {
    @Resource
    RoleService roleService=null;
    @ApiOperation(value = "查看所有角色的操作",
    notes = "请查看",//response = Role.class,
    httpMethod = "POST")
    //httpmethod请求方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "大小长度")
    })
    @PostMapping("/getall")
//    @ResponseBody
    public JsonResult get(@RequestParam(value = "page",defaultValue = "1",required = false) Integer page,@RequestParam(value = "size",defaultValue = "5",required = false) Integer size) throws Exception{
        JsonResult jsonResult=new JsonResult();
        List<Role> selectall = roleService.selectall((page-1)*size,size);
        if(selectall!=null){
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(selectall);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据角色名查询的操作")
    //httpmethod请求方式
    @PostMapping(value = "/select")
//    @ResponseBody
    public JsonResult selectbyname(@RequestParam(value = "name") String name) throws Exception{
        JsonResult jsonResult=new JsonResult();
        List<Role> selectbyname = roleService.selectbyname("%"+name+"%");
        if(selectbyname!=null){
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(selectbyname);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "新增角色的操作",
            notes = "请查看",//response = Role.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/add")
            @ApiImplicitParam(name = "role",value = "Role 角色类")
    @ResponseBody
    public JsonResult addrole(@RequestBody Role role) throws Exception{
        JsonResult jsonResult=new JsonResult();
        Integer insert = roleService.insert(role);
        if(insert>=1){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
            jsonResult.setData(insert);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败,发生错误");
        }
        return jsonResult;
    }

    @ApiOperation(value = "编辑角色的操作",
            notes = "请查看",//response = Role.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/update")
            @ApiImplicitParam(name = "role",value = "Role 角色类")
    @ResponseBody
    public JsonResult updaterole(@RequestBody Role role) throws Exception{
        JsonResult jsonResult=new JsonResult();
        Integer update = roleService.update(role);
        if(update>=1){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
            jsonResult.setData(update);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据角色id删除该角色的操作",
            notes = "请查看",//response = Role.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/delete")
            @ApiImplicitParam(name = "id",value = "Roleid")
    @ResponseBody
    public JsonResult deletebyid(@RequestParam Integer[] id) throws Exception{
        JsonResult jsonResult=new JsonResult();
        Integer delete = roleService.delete(id);
        if(delete>=1){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
            jsonResult.setData(delete);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }
}